package org.projects.graduates.page;

/**
 * commons-java-pager - common pager components for Java Web Develop - http://code.google.com/p/common-java-pager/
 * Copyright (C) 2008-2010 <a href="http://tomaer.com">tomaer</a>
 * 
 * == BEGIN LICENSE ==
 * 
 * Licensed under the terms of any of the following licenses at your choice:
 * 
 *  - Apache License, Version 2.0
 *    http://http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  - Creative Commons 3.0 BY-SA
 *    http://http://creativecommons.org/licenses/by-sa/3.0/
 * 
 * == END LICENSE ==
 */

import java.io.Serializable;

public final class Pager implements Serializable {

	private static final long serialVersionUID = -7742988063989166270L;

	/** first page */
	private long firstPage;

	/** last page */
	private long lastPage;

	/** next page */
	private long nextPage;

	/** prev page */
	private long prevPage;

	/** current page */
	private long currentPage;

	/** total page */
	private long totalPage;

	/** total count */
	private long rowCount;

	/** page size */
	private long pageSize;

	// 计算剩余行数
	private long surplusLine;

	/** exists next page */
	private boolean hasNext;

	/** exists prev page */
	private boolean hasPrev;

	/** exists first page */
	private boolean hasFirst;

	/** exists last page */
	private boolean hasLast;

	/** Constructor default */
	@SuppressWarnings("unused")
	private Pager() {
	}

	/**
	 * 
	 * @param rowCount 记录的条数
	 * @param pageSize
	 * @param currentPage
	 */
	public Pager(long rowCount, long pageSize, long currentPage) {
		this.rowCount = rowCount;
		this.currentPage = currentPage;
		this.pageSize = pageSize;
		this.totalPage = this.rowCount % pageSize == 0 ? this.rowCount / pageSize : this.rowCount / pageSize + 1;
		this.surplusLine = this.rowCount - (this.currentPage - 1) * this.pageSize;

		if (this.totalPage > 0) {
			this.hasFirst = true;
			this.firstPage = 1;
		}
		if (this.currentPage > 1) {
			this.hasPrev = true;
			this.prevPage = this.currentPage - 1;
		}
		if (this.totalPage > 0 && this.currentPage < this.totalPage) {
			this.hasNext = true;
			this.nextPage = this.currentPage + 1;
		}
		if (this.totalPage > 0) {
			this.hasLast = true;
			this.lastPage = this.totalPage;
		}

	}

	public long startLine() {
		long result = (this.currentPage - 1) * this.pageSize;
		return result <= 0 ? 0 : result;
	}

	@Override
	public String toString() {
		return "totalPage " + this.totalPage + " rowCount " + rowCount;
	}

	public long getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(long totalPage) {
		this.totalPage = totalPage;
	}

	public long getFirstPage() {
		return firstPage;
	}

	public void setFirstPage(long firstPage) {
		this.firstPage = firstPage;
	}

	public long getLastPage() {
		return lastPage;
	}

	public void setLastPage(long lastPage) {
		this.lastPage = lastPage;
	}

	public long getNextPage() {
		return nextPage;
	}

	public void setNextPage(long nextPage) {
		this.nextPage = nextPage;
	}

	public long getPrevPage() {
		return prevPage;
	}

	public void setPrevPage(long prevPage) {
		this.prevPage = prevPage;
	}

	public long getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(long currentPage) {
		this.currentPage = currentPage;
	}

	public long getRowCount() {
		return rowCount;
	}

	public void setRowCount(long rowCount) {
		this.rowCount = rowCount;
	}

	public long getPageSize() {
		return pageSize;
	}

	public void setPageSize(long pageSize) {
		this.pageSize = pageSize;
	}

	public long getSurplusLine() {
		return surplusLine;
	}

	public void setSurplusLine(long surplusLine) {
		this.surplusLine = surplusLine;
	}

	public boolean isHasNext() {
		return hasNext;
	}

	public void setHasNext(boolean hasNext) {
		this.hasNext = hasNext;
	}

	public boolean isHasPrev() {
		return hasPrev;
	}

	public void setHasPrev(boolean hasPrev) {
		this.hasPrev = hasPrev;
	}

	public boolean isHasFirst() {
		return hasFirst;
	}

	public void setHasFirst(boolean hasFirst) {
		this.hasFirst = hasFirst;
	}

	public boolean isHasLast() {
		return hasLast;
	}

	public void setHasLast(boolean hasLast) {
		this.hasLast = hasLast;
	}

}
